<?php
include("../contents.php");
include("../security.php");
include("../settings.php");
include("../database.php");
include("../navigation.php");

if (!empty($_GET["name"])) {
	$pageprops=mysql_query("SELECT * FROM ". $GLOBALS["db_pref"] ."content_documents WHERE id2='". $_GET["name"] ."'");
	if (mysql_num_rows($pageprops)==1) {
		$row_page=mysql_fetch_object($pageprops);
		$docdir=mysql_query("SELECT * FROM ". $GLOBALS["db_pref"] ."content_dirs WHERE id='". $row_page->ofdir ."'");
		if (mysql_num_rows($docdir)==1) {
			$row_dir=mysql_fetch_object($docdir);
			if (Contents::presence_rights($row_dir->presence)) {
				if (!empty($_GET["createnewshortname"])) {
					$shortname=Contents::create_unique_docname($row_page->webtitle, "content_documents", $row_dir->presence);
					if (mysql_query("UPDATE ". $GLOBALS["db_pref"] ."content_documents SET shortname='". $shortname ."' WHERE id2='". $_GET["name"] ."'")) {
						header("Location:". $_SERVER["PHP_SELF"] ."?name=". $row_page->id2);
						exit();
					}
				}
			} else {
				header("Location:/admin_content_overview.php?dir=0");
				exit();
			}
		} else {
			header("Location:/admin_content_overview.php?dir=0");
			exit();
		}
	} else {
		header("Location:/admin_content_overview.php?dir=0");
		exit();
	}
} else {
	header("Location:/admin_content_overview.php?dir=0");
	exit();
}

if (!empty($_POST)) {
	if (!empty($_GET["name"])) {
		$usergroups="";
		if(!empty($_POST["webusergroups"]) && is_array($_POST["webusergroups"])) {
			$usergroups=implode(",", $_POST["webusergroups"]);
		}
		if (mysql_query("UPDATE ". $GLOBALS["db_pref"] ."content_documents SET pagetitle='". $_POST["pagetitle"] ."', webtitle='". $_POST["webtitle"] ."', cache='". $_POST["caching"] ."', webusergroups='". $usergroups ."', editlang='". $_POST["language"] ."', template='". $_POST["template"] ."' WHERE id2='". $_GET["name"] ."'")) {
			header("Location:/admin_content_edit.php?name=". $_GET["name"]);
			exit();
		} else {
			echo mysql_error();
		}
	}
}
echo "<"."?xml version=\"1.0\"?".">";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title><?php echo getsetting("sitename"); ?> - Administration</title>
    <link media="screen" rel="stylesheet" href="css/style.css" type="text/css" />
    <script src="js/scripts.js" type="text/javascript"></script>
  </head>

  <body id="edit">
    <?php
    $GLOBALS["linkzusatz"]="";
    if (!empty($_GET["name"])) {
    	$GLOBALS["linkzusatz"].="&name=". $_GET["name"];
    }
    ?>
    <form id="linkform" action="<?php echo $_SERVER["PHP_SELF"]; ?>?<?php echo $GLOBALS["linkzusatz"]; ?>" method="post">
    <p id="pageheadline">Seitentitel:</p>
    <input type="text" value="<?php echo $row_page->pagetitle; ?>" name="pagetitle" size="40" maxlength="255" />
    <p id="pageheadline">Webtitel:</p>
    <input type="text" value="<?php echo $row_page->webtitle; ?>" name="webtitle" size="40" maxlength="255" />
    <p id="pageheadline">Kurzname (<a href="<?php echo $_SERVER["PHP_SELF"]; ?>?name=<?php echo $row_page->id2 ?>&amp;createnewshortname=1">neu erzeugen</a>):</p>
    <div><?php echo $row_page->shortname; ?></div>
    <p id="pageheadline">Seite cachen:</p>
    	<div>
    	<input type="radio" value="1" id="cacheyes" name="caching" <?php echo ($row_page->cache==1)?" checked=\"checked\"":""; ?> /> <label for="cacheyes">Ja</label>
    	<input type="radio" value="0" id="cacheno" name="caching" <?php echo ($row_page->cache==0)?" checked=\"checked\"":""; ?> /> <label for="cacheno">Nein</label>
    	</div>
    <p id="pageheadline">Zugriff auf diese Seite:</p>
    	<div>
    		<select name="webusergroups[]" size="3" multiple="multiple">
   			<?php
   				$act_groups=array();
   				if($row_page->webusergroups!="") {
   					$expl_webusergroups=explode(",", $row_page->webusergroups);
   					for($j=0; $j<count($expl_webusergroups); $j++) {
   						$act_groups[$expl_webusergroups[$j]]=1;
   					}
   				}
    			$webusergroups=mysql_query("SELECT * FROM ". $GLOBALS["db_pref"] ."webusers_groups WHERE presence='". $row_dir->presence ."'");
    			for($i=0; $i<mysql_num_rows($webusergroups); $i++) {
    				$row_group=mysql_fetch_object($webusergroups);
    				?>
    				<option <?php if(isset($act_groups[$row_group->id])) { echo "selected=\"selected\"";} ?> value="<?php echo $row_group->id; ?>"><?php echo $row_group->name; ?></option>
    				<?php
    			}
    			?>
    		</select>
    	</div>
    <p id="pageheadline">Sprache bearbeiten:</p>
    	<div>
    		<select name="language" size="1">
   			<?php
   				$act_lang=0;
   				if($row_page->editlang!="") {
   					$act_lang=$row_page->editlang;
   				}
    			$languages=mysql_query("SELECT * FROM ". $GLOBALS["db_pref"] ."languages");
    			for($i=0; $i<mysql_num_rows($languages); $i++) {
    				$row_lang=mysql_fetch_object($languages);
    				?>
    				<option <?php if($act_lang==$row_lang->shortname) { echo "selected=\"selected\"";} ?> value="<?php echo $row_lang->shortname; ?>"><?php echo $row_lang->name; ?></option>
    				<?php
    			}
    			?>
    		</select>
    	</div>
    <p id="pageheadline">Template:</p>
    	<div>
    		<select name="template" size="1">
    			<option value="">--- Bitte w&auml;hlen ---</option>
   			<?php
   				$act_template=0;
   				if($row_page->template!="") {
   					$act_template=$row_page->template;
   				}
   				$dirname="../". getsetting("templatedir");
				$dir=opendir($dirname);
				while (false!==($file=readdir($dir))) {
					if ($file!="." && $file!=".." && is_file($dirname ."/". $file) && substr($file,((strlen($file)) - 4),((strlen($file))))==".xsl" && $file!="CVS" && !stristr(".svn", $file) && substr($file,0,1)!="_") {
						$name=substr($file,0,((strlen($file)) - 4));
						?><option <?php if($act_template==$file) { echo "selected=\"selected\"";} ?> value="<?php echo $file; ?>"><?php echo $name; ?></option><?php
					}
				}
				closedir($dir);
    			?>
    		</select>
    	</div>
    <p id="pageheadline"><input type="submit" value="Speichern" /></p>
   	</form>
  </body>
</html>